//
//  DHRegisterViewController.m
//  DHKuaiYi
//
//  Created by Cedric Zheng on 15/11/2016.
//  Copyright © 2016 30-陈辉华. All rights reserved.
//

#import "DHRegisterViewController.h"


@interface DHRegisterViewController ()

/**
 用户协议前面的圆形按钮
 */
@property (weak, nonatomic) UIButton *roundButton;

/**
 验证码输入栏
 */
@property (weak, nonatomic) UITextField *verificationTextField;

/**
 电话号码验证图
 */
@property (weak, nonatomic) UIImageView *phoneNumVeriImageView;

/**
 密码验证图
 */
@property (weak, nonatomic) UIImageView *passwordVeriImageView;

@end

@implementation DHRegisterViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*----设置导航栏----*/
    self.title = @"注册";
    
    [self.view setBackgroundColor:[UIColor redColor]];
}

- (void)xhhSetupSubviews {
    [super xhhSetupSubviews];
    
    /*----设置用户名栏----*/
//    //为用户名栏添加编辑监听事件
//    [self.userNameTextField addTarget:self action:nil forControlEvents:UIControlEventEditingDidEnd];
//    [self.userNameTextField addTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
    
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-100.0);
    }];
    
    /*----设置密码栏----*/
//    //为密码栏添加编辑监听事件
//    [self.passwordTextField addTarget:self action:nil forControlEvents:UIControlEventEditingDidEnd];
//    [self.passwordTextField addTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
    
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.userNameTextField);
        make.top.equalTo(self.userNameTextField.mas_bottom).offset(10.0);
    }];
    
    /*----验证码输入栏----*/
    UITextField *verificationTextField = [self getTextFieldWithLeftFieldImageName:@"login_invition" placeholder:@"请输入验证码"];
    verificationTextField.keyboardType = UIKeyboardTypeNumberPad;
    verificationTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.verificationTextField = verificationTextField;
    [self.view addSubview:verificationTextField];
    [verificationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameTextField);
        make.right.equalTo(self.userNameTextField.mas_centerX);
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(10.0);
    }];
    
    /*----获取验证码按钮----*/
    UIButton *getVeriCodeButton = [UIButton new];
    [getVeriCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getVeriCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [getVeriCodeButton setBackgroundImage:[UIImage imageNamed:@"buttonBackground"] forState:UIControlStateNormal];
    getVeriCodeButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    
    [self.view addSubview:getVeriCodeButton];
    [getVeriCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameTextField.mas_centerX).offset(10.0);
        make.top.bottom.equalTo(verificationTextField);
        make.right.equalTo(self.userNameTextField);
    }];
    
    /*----用户协议----*/
    UIButton *agreementButton = [[UIButton alloc] init];
    [agreementButton setTitle:@"本人已同意并阅读用户协议" forState:UIControlStateNormal];
    [agreementButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    agreementButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    
    [self.view addSubview:agreementButton];
    [agreementButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(verificationTextField.mas_bottom).offset(20.0);
    }];
    
    /*----用户协议确认圆形按钮----*/
    UIButton *roundButton = [[UIButton alloc] init];
    [roundButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [roundButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    self.roundButton = roundButton;
    [roundButton addTarget:self action:@selector(roundButtonDidTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:roundButton];
    [roundButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(agreementButton);
        make.right.equalTo(agreementButton.mas_left).offset(-8.0);
    }];
    
    /*----设置注册按钮----*/
    //把父类的绿色按钮当注册按钮使用
    UIButton *registerButton = self.aGreenButton;
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerButtonDidTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.userNameTextField);
        make.top.equalTo(agreementButton.mas_bottom).offset(20.0);
    }];
    
    //手机号码验证图
    UIImageView *phoneNumVeriImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_right"]];
    phoneNumVeriImageView.hidden = YES;
    
    self.phoneNumVeriImageView = phoneNumVeriImageView;
    [self.view addSubview:phoneNumVeriImageView];
    [phoneNumVeriImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameTextField.mas_right).offset(4.0);
        make.centerY.equalTo(self.userNameTextField);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    //密码验证图
    UIImageView *passwordVeriImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_right"]];
    passwordVeriImageView.hidden = YES;
    
    self.passwordVeriImageView = passwordVeriImageView;
    [self.view addSubview:passwordVeriImageView];
    [passwordVeriImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordTextField.mas_right).offset(4.0);
        make.centerY.equalTo(self.passwordTextField);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

#pragma mark - Events
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //退出键盘
    [self.view endEditing:YES];
}

- (void)roundButtonDidTapped {
    self.roundButton.selected = !self.roundButton.selected;
}

- (void)registerButtonDidTapped {
    //判断是否同意协议
    if (self.roundButton.selected == NO) {
        [SVProgressHUD showErrorWithStatus:@"请同意用户协议"];
        delay(1.25);
        return;
    }
    
    if ([self.passwordTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        delay(1.25);
        return;
    }
    
    if ([self.userNameTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        delay(1.25);
        return;
    }
    
    [SVProgressHUD showSuccessWithStatus:@"注册成功！"];
    delay(1.5);
    
    if (![XHHUserDefaultsTool objetForKey:USER_ACCOUNT_KEY]) {
        NSMutableArray *accountInfo = [[NSMutableArray alloc] init];
        [XHHUserDefaultsTool saveObject:accountInfo forKey:USER_ACCOUNT_KEY];
    }
    
    //保存用户信息到默认设置
    NSDictionary *accountDict = @{@"user":self.userNameTextField.text, @"password":self.passwordTextField.text};
    NSMutableArray* newAccountInfo = [NSMutableArray arrayWithArray:[XHHUserDefaultsTool objetForKey:USER_ACCOUNT_KEY]];
    [newAccountInfo addObject:accountDict];
    [XHHUserDefaultsTool saveObject:newAccountInfo forKey:USER_ACCOUNT_KEY];
    
    //发送一个登陆状态发生改变的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LOGIN_STATUS_CHANGED object:nil];
    
    //移除当前控制器
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//- (void)textFieldDidChangeEditing {
//    
//    
//}

#pragma mark – Private Methods

#pragma mark - <#Delegates#>

#pragma mark - <#Datasources#>

#pragma mark – Getters and Setters
@end
